package com.note.app.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.note.common.vo.ResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Package：com.note.config.config
 * @Name：CustomBlockHandler
 * @Author：热伊木
 * @Email：uyevan@163.com
 * @Date：2024-04-30-8:30
 * @Description：自定义Sentinel拦截层
 */
@Component("SentinelBlockHandler")
public class SentinelBlockHandler {
    /**
     * 限流返回
     *
     * @param exception
     * @return
     */
    @ResponseBody
    public static ResponseVo<Void> blockHandleException(BlockException exception) {
        return ResponseVo.error(429, "频繁请求,请稍后再试");
    }

    /**
     * 熔断设置
     *
     * @param userDto
     * @param throwable
     * @return
     */
    @ResponseBody
    public static ResponseVo<Void> fallBackHandle(Throwable throwable) {
        return ResponseVo.error(503, "系统访问量过高，请稍后再试");
    }

    /**
     * 热点规则
     *
     * @param userDto
     * @param exception
     * @return
     */
    @ResponseBody
    public static ResponseVo<Void> hotBackHandle(BlockException exception) {
        return ResponseVo.error(403, "恶意访问,请稍后再试");
    }
}
